/* Constructor */
BelScrollbar						= {}

/* Initialization */
BelScrollbar.Initialize				= function( self, ... )

	/* Register as BelControl */
	BelControls.Register( self, DPanel, ... )
	
	/* Default values */
	self.Direction					= LIST_DIR_V
	self:SetText( "" )
	self.Margin						= { l=8, r=8, t=8, b=8 }
	self.IncrustBorder	= false
	self.CornerSizeRatio			= 0.01
	self.ScrollerColor				= Color( 75, 75, 75, 200 )
	self.CurrentPos					= { x=0, y=0 }
end

/* Update layout */
BelScrollbar.UpdateLayout			= function( self )

	/* Update size & position */
	if self.Direction == LIST_DIR_V then
	
		/* Size */
		local h = self:GetParent():GetMaxTall() - self:GetParent().Padding.t - self:GetParent().Padding.b - self.Margin.t - self.Margin.b
		local w = self:GetParent().ScrollAreaSize.w - self.Margin.l - self.Margin.r
		
		/* Position */
		local x = self:GetParent():GetMaxWide() - self:GetParent().ScrollAreaSize.w - self:GetParent().Padding.r + self.Margin.l
		local y = self:GetParent().Padding.t + self.Margin.t
		
		self:SetMaxWide( w )
		self:SetMaxTall( h )
		self:SetFinalPos( x, y )
	
		/* Debug output */
		DEBUG:Msg( "Size: " .. w .. ", " .. h .. " - Pos: " .. x .. ", " .. y, "BelScrollbar.UpdateLayout" )
		
	else
	
		/* Debug output */
		DEBUG:Msg( "Setting scrollbar to horizontal", "BelScrollbar.UpdateLayout" )
		
		/* Size */
		self:SetMaxWide( self:GetParent():GetMaxWide() - self:GetParent().Padding.l - self:GetParent().Padding.r - self.Margin.l - self.Margin.r )
		self:SetMaxTall( self:GetParent().ScrollAreaSize.h - self.Margin.t - self.Margin.b )
		
		/* Position */
		self:SetFinalPos(
			self:GetParent().Padding.l + self.Margin.l,
			self:GetParent():GetMaxTall() - self:GetParent().ScrollAreaSize.h - self:GetParent().Padding.b + self.Margin.t )
	end
end

/* Change direction */
BelScrollbar.SetDirection			= function( self, value )

	/* Invalid direction */
	if value != LIST_DIR_V and value != LIST_DIR_H then
		DEBUG:ErrMsg( "Invalid direction specified", "BelScrollbar.SetDirection" )
		return
	end
	
	/* Debug output */
	DEBUG:Msg( "Updating scrollbar direction from " .. self.Direction .. " to " .. value, "BelScrollbar.SetDirection" )
	
	/* Nothing else if direction hasn't changed */
	if self.Direction == value then return end
	
	/* Change the direction */
	self.Direction					= value
	
	/* Update the layout */
	self:UpdateLayout()
end

/* Paint over control */
BelScrollbar.PaintOver				= function( self )

	/* Only once initialized */
	if not self.IsRegistered then return end

	/* Get the current color */
	local fadeValue					= self.ShowHideFader:GetCurrentValue()
	local alpha						= math.min( self.ScrollerColor.a, fadeValue.Alpha )
	local addX						= self.CurrentPos.x
	local addY						= self.CurrentPos.y
	
	/* Scroller size */
	local size						= 0
	if self.Direction == LIST_DIR_V then
		size						= self:GetParent():GetPercentVisible() * self:GetMaxTall()
	else
		size						= self:GetParent():GetPercentVisible() * self:GetMaxWide()
	end
	
	/* Draw vertical scroller */
	if self.Direction == LIST_DIR_V then
		surface.SetDrawColor( self.ScrollerColor.r, self.ScrollerColor.g, self.ScrollerColor.b, alpha )
		surface.DrawRect( 0, addY, self:GetMaxWide(), size )
		surface.SetDrawColor( 0, 0, 0, alpha )
		surface.DrawOutlinedRect( 2, size / 2 - 6 + addY, self:GetMaxWide() - 4, 12 )
		surface.DrawLine( 4, size / 2 - 4 + addY, self:GetMaxWide() - 4, size / 2 - 4 + addY )
		surface.DrawLine( 4, size / 2 - 2 + addY, self:GetMaxWide() - 4, size / 2 - 2 + addY )
		surface.DrawLine( 4, size / 2 + addY, self:GetMaxWide() - 4, size / 2 + addY )
		surface.DrawLine( 4, size / 2 + 2 + addY, self:GetMaxWide() - 4, size / 2 + 2 + addY )
		surface.DrawLine( 4, size / 2 + 4 + addY, self:GetMaxWide() - 4, size / 2 + 4 + addY )
	
	/* Draw horizontal scroller */
	else
		surface.SetDrawColor( self.ScrollerColor.r, self.ScrollerColor.g, self.ScrollerColor.b, alpha )
		surface.DrawRect( addX, 0, size, self:GetMaxTall() )
		surface.SetDrawColor( 0, 0, 0, alpha )
		surface.DrawOutlinedRect( size / 2 - 6 + addX, 2, 12, self:GetMaxTall() - 4 )
		surface.DrawLine( size / 2 - 4 + addX, 4, size / 2 - 4 + addX, self:GetMaxTall() - 4 )
		surface.DrawLine( size / 2 - 2 + addX, 4, size / 2 - 2 + addX, self:GetMaxTall() - 4 )
		surface.DrawLine( size / 2 + addX, 4, size / 2 + addX, self:GetMaxTall() - 4 )
		surface.DrawLine( size / 2 + 2 + addX, 4, size / 2 + 2 + addX, self:GetMaxTall() - 4 )
		surface.DrawLine( size / 2 + 4 + addX, 4, size / 2 + 4 + addX, self:GetMaxTall() - 4 )
	end
end

BelScrollbar.OnCursorMoved			= function( self, x, y )
	if self.CurrentControlState == CTRL_STATE_DOWN then
		self.CurrentPos.x			= self.CurrentPos.x + ( x - self.CurrentPos.x ) * 0.1
		self.CurrentPos.y			= self.CurrentPos.y + ( y - self.CurrentPos.y ) * 0.1
		-- self.CurrentPos.x			= math.max( 0, math.min( self:GetMaxWide() - self:GetParent():GetPercentVisible() * self:GetMaxWide(), self.CurrentPos.x ) )
		-- self.CurrentPos.y			= math.max( 0, math.min( self:GetMaxTall() - self:GetParent():GetPercentVisible() * self:GetMaxTall(), self.CurrentPos.y ) )
		self:UpdateLayout()
	end
end

/* Register the control */
vgui.Register( "BelScrollbar", BelScrollbar, "DPanel" )